import { observable, action, runInAction } from 'mobx';
import { Http } from '../services/Http';
import { getAuthObjectTree } from '../basicData/api';
import { authObjectTreeRecombinant, } from '../util/util';

namespace common {
  export const http: Http = new Http();
}

export class AuthObjectStore {

  // 用于角色绑定
  @observable public allAuthObject: Array<{}>;
  @observable public authObjectTree: Array<{}>;
  
  @observable public authObjectTreeCount: number;

  constructor() {
    this.authObjectTreeCount = 0;
    this.authObjectTree = []
  }


  @action getAuthObjectTree = () => {
    getAuthObjectTree().then(
      (res: any) => {
        runInAction(() => {
          this.authObjectTreeCount = res.result.length ? res.result.length : 1
          this.authObjectTree = authObjectTreeRecombinant(res.result.length ? res.result : [res.result])
        })
      }
    )
  }
}